package com.novunda.platform.repositories;

import com.novunda.platform.common.repositories.BaseRepository;
import com.novunda.platform.entities.Role;
import com.novunda.platform.entities.base.BaseEntity;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * .
 * 功能说明：角色dao
 * @author linfeng
 * @see 2016年9月22日
 * @version 1.0
 * @modify FJ
 */
public interface RoleRepository extends BaseRepository<Role, Long> {

	/**
	 * .
	 *功能说明：根据名称查询
	 * @param name
	 * @return
	 */
    @Query("from Role where name = ?1 and delFlag = '" + BaseEntity.DEL_FLAG_NORMAL + "'")
    Role findByName(String name);

    /**
     * .
     *功能说明：删除
     * @param id
     * @return
     */
    @Modifying
    @Query("update Role set delFlag='" + BaseEntity.DEL_FLAG_DELETE + "' where id = ?1")
    int deleteById(Long id);
    
    /**
     * .
     *功能说明：根据名称模糊查询
     * @param name
     * @return
     */
    @Query("from Role where name like %?1% and delFlag = '" + BaseEntity.DEL_FLAG_NORMAL + "'")
    List<Role> findByLikeName(String name);

    /**
     * .
     *功能说明：根据名称和机构查询
     * @param name 角色名称
     * @param officeId 机构id
     * @return
     */
    @Query("from Role where name = ?1 and office.id = ?2 and delFlag = '" + BaseEntity.DEL_FLAG_NORMAL + "'")
    Role findByNameAndOffice(String name, long officeId);
    
    /**
     * .
     *功能说明：根据机构查询
     * @param officeId 机构id
     * @return
     */
    @Query("from Role where office.id = ?1 and delFlag = '" + BaseEntity.DEL_FLAG_NORMAL + "'")
    List<Role> findByOffice(long officeId);
    
    /**
     * .
     *功能说明：根据id查询角色信息
     * @param id 角色id
     * @return
     */
    @Query("from Role where id = ?1 and delFlag = '" + BaseEntity.DEL_FLAG_NORMAL + "'")
    Role get(Long id);
}
